package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import java.util.Arrays;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Fengkai 1 2两个字符串 找出两者的交集并按顺序输出
 * @creat 2023/3/30
 */
public class FindIntersection {

       public static void main(String[] args){
           System.out.println(myMethod("bach", "bbaaccddfg"));
       }

    public static String myMethod(String first, String second){
        char[] chars1 = first.toCharArray();
        char[] chars2 = second.toCharArray();
        Set<Character> characters = new TreeSet<>();
        for (char c : chars1) {
            characters.add(c);
        }
        String s1 = "";
        for (Character c:characters) {
            s1 += c;
        }
        characters.clear();
        for (char c : chars2) {
            characters.add(c);
        }
        String s2 ="";
        for (Character c:characters) {
            s2 += c;
        }
        String out = "";
        for (int i = 0; i < s1.length(); i++) {
           if (s2.contains(String.valueOf(s1.charAt(i)))){
               out +=String.valueOf(s1.charAt(i));
           }
        }
        return out;
    }
}
